package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.common.SaveAddress;
import com.linzp.bishe.entity.Meeting;
import com.linzp.bishe.mapper.MeetingMapper;
import com.linzp.bishe.service.MeetingService;
import com.linzp.bishe.utils.DealFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {
	@Autowired
	private SaveAddress saveAddress;

	@Autowired
	private MeetingMapper meetingMapper;

	@Override
	public String updateMultipartFile(MultipartFile file, String fileName) {
		String name = DealFile.upDateMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getMeeting(), fileName);
		if (!name.equals(fileName)){
			DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getCache()+"/"+fileName);
		}
		return name;
	}

	@Override
	public void getByIdAllMeeting(Result success,Integer id, Integer current, Integer page, String searchMeeting, String searchStatus) {
		Page<Meeting> meetingPage = new Page<>(current,page);
		QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();
		if (id!=null) queryWrapper.eq("user_id",id);
		if (searchMeeting!=null & !searchMeeting.isEmpty())queryWrapper.like("meeting_name",searchMeeting);
		if (searchStatus!=null & !searchStatus.isEmpty())queryWrapper.eq("status",searchStatus);
		meetingPage = this.page(meetingPage,queryWrapper);
		success.put("tableData",meetingPage.getRecords());
		success.put("total",meetingPage.getTotal());
		success.put("loadAddress",saveAddress.getAccessPath()+saveAddress.getMeeting());
	}

	@Override
	public void deleteMeetingById(int id, String fileName) {
		meetingMapper.deleteById(id);
		if (fileName!=null){
			if(fileName.isEmpty()){
				DealFile.DeleteFolder(saveAddress.getAllAddress()+
						saveAddress.getMeeting()+"/"+fileName);
			}
		}
	}

	@Override
	public String saveMultipartFile(MultipartFile file) {
		return DealFile.saveMultipartFile(file,saveAddress.getAllAddress()+saveAddress.getCache());
	}

	@Override
	public void saveCacheToFile(String name) {
		DealFile.saveCacheToFile(saveAddress.getAllAddress()+saveAddress.getCache(),
				saveAddress.getAllAddress()+saveAddress.getMeeting(),name);
		DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getCache()+"/"+name);
	}

	/**
	 * @Description 处理状态
	 * @Param [id, status]
	 * @return void
	 */
	@Override
	public void dealStatus(Integer id, String status) {
		UpdateWrapper<Meeting> wrapper = new UpdateWrapper<>();
		wrapper.eq("id",id);
		wrapper.set("status",status);
		this.update(wrapper);
	}

	@Override
	public boolean cheakRepeat(String element1,Integer element2) {
		QueryWrapper<Meeting> wrapper = new QueryWrapper<>();
		wrapper.eq("meeting_name",element1);
		wrapper.eq("user_id",element2);
		return this.getOne(wrapper,false)!=null ? true:false;
	}
}
